/* education 项目专用js */


var courseStatus = ['直播', '录播', '考试', '实战'];
//用户类型
var userType = '';
//用户职责
var userDuty = '';
//学校专业
var speciality = '';
//教学计划分类
var plan_classify = '';


function getUserDuty() {
    if (userDuty == '') {
        userDuty = [];
        $.ajax({
            type: 'get',
            url: context + "/api/sysDuty/list",
            success: function (response) {
                var data = response.data;
                if (typeof data == 'object') {
                    for (var i = 0, n = data.length; i < n; i++) {
                        userDuty[data[i].duty_id] = data[i].dutyName;
                    }
                    console.log(userDuty);
                }
            }
        });
    }
    return userDuty;
}

function getUserRole() {
    if (userType == '') {
        userType = [];
        $.ajax({
            type: 'get',
            url: context + "/api/sysrole/list",
            success: function (response) {
                var data = response.data;
                if (typeof data == 'object') {
                    for (var i = 0, n = data.length; i < n; i++) {
                        userType[data[i].role_id] = data[i].roleName;
                    }
                    console.log(userType);
                }
            }
        });
    }
    return userType;
}

function getPlanClaasify() {
    if (plan_classify == '') {
        plan_classify = [];
        $.ajax({
            type: 'get',
            url: context + '/api/eduCoursePlanClassify/list',
            success: function (response) {
                var data = response.data;
                if (typeof data == 'object') {
                    for (var i = 0, n = data.length; i < n; i++) {
                        plan_classify[data[i].plan_classify_id] = data[i].title;
                    }
                    console.log(plan_classify);
                }
            }
        });
    }
    return plan_classify;
}

function getSpeciality() {
    if (speciality == '') {
        speciality = [];
        $.ajax({
            type: 'get',
            url: context + '/api/eduSchoolSpeciality/list',
            success: function (response) {
                var data = response.data;
                if (typeof data == 'object') {
                    for (var i = 0, n = data.length; i < n; i++) {
                        speciality[data[i].speciality_id] = data[i].title;
                    }
                    console.log(speciality);
                }
            }
        });
    }
    return speciality;
}

getUserDuty();
getUserRole();
getPlanClaasify();
getSpeciality();


//弹出分类选择页面,三个参数,表单ID,表单值ID,表单显示名称ID
function showMajorTree(clickId, parent_id, parent_title) {
    $("#" + clickId).click(function () {
        showDialog({
            title: '选择父级菜单'
            , template: context + '/views/course/category/template/per.html'
            , width: 400
            , height: 600
            , yes: function (index, layero) {
                var res = callbackdata();
                layer.close(index);
                $("#" + (parent_id || "parent_id")).val(res.value);
                $("#" + (parent_title || "parent_title")).val(res.title);
                $("#majorCode").val(res.sourceObj.majorCode);
            }
        });
    });
}

/*选择全部学校*/
function selectAllSchool(schoolNameId, schoolCodeId) {
    selectSchool(schoolNameId, schoolCodeId, context + '/api/eduSchoolAll/list');
}

/*选择已有学校*/
function selectSchool(schoolNameId, schoolCodeId, url) {
    layui.use(['tableSelect'], function () {
        var tableSelect = layui.tableSelect;
        var schoolName = schoolNameId || '_schoolName';
        var schoolCode = schoolCodeId || "_schoolCode";
        tableSelect.render({
            elem: '#' + (schoolName) + ',#' + (schoolCode),	//定义输入框input对象 必填
            checkedKey: 'schoolCode', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'schoolName',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '学校名称',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: url || (context + '/api/school/list')
                , request: {
                    limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                cols: [[
                    // { type: 'checkbox' },//多选
                    {type: 'radio'},//单选
                    {field: 'schoolCode', title: '代码', width: 120},
                    {field: 'schoolName', title: '名称', width: 300},
                    {field: 'location', title: '地区', width: 100}
                ]]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                console.log(data);
                data = data.data[0];
                $('#' + schoolCode).val(data.schoolCode);
                $('#' + schoolName).val(data.schoolName);
            }
        })
    });
}

/*选择班级*/
function selectSquad(squadNameId, squadCodeId, schoolCode) {
    layui.use(['tableSelect'], function () {
        var tableSelect = layui.tableSelect;
        var squadName = squadNameId || '_squadName';
        var squadCode = squadCodeId || "_squadCode";
        tableSelect.render({
            elem: '#' + (squadName) + ',#' + (squadCode),	//定义输入框input对象 必填
            checkedKey: 'squadCode', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'squad_name',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '班级名称',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: (context + '/api/squad/list?schoolCode=' + (schoolCode || ''))
                , request: {
                    limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                cols: [[
                    // {type: 'checkbox'},//多选
                    { type: 'radio' },//单选
                    {field: 'squadCode', title: '代码', width: 120},
                    {field: 'squad_name', title: '名称', width: 300},
                    {
                        title: '专业', width: 100, templet: function (d) {
                            return getSpeciality()[d.speciality_id];
                        }
                    }
                ]]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                console.log(data);
                data = data.data[0];
                $('#' + squadCode).val(data.squadCode);
                $('#' + squadName).val(data.squad_name);
            }
        })
    });
}

/*根据学校选择 学生 */
function selectStuFromSquadCode(showId, showName, schoolCode) {
    layui.use(['tableSelect'], function () {
    var tableSelect = layui.tableSelect;
    showName = showName || '_nickname';
    showId = showId || "_user_id";
    tableSelect.render({
        elem: '#' + (showName) + ',#' + (showId),	//定义输入框input对象 必填
        checkedKey: 'user_id', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'nickname',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '班级名称',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url: context + '/api/sysuser/getlist?schoolCode=' + (schoolCode||'1')
            , request: {
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            cols: [[
                {type: 'checkbox'},//多选
                // { type: 'radio' },//单选
                {field: 'user_id', title: '代码', width: 120},
                {field: 'nickname', title: '名称', width: 300},
                {
                    title: '班级', width: 100, templet: function (d) {
                        if(d.get_eduSquad)return d.get_eduSquad.squad_name;else return '';
                    }
                }
            ]]
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            console.log(data);
            data = data.data[0];
            $('#' + showId).val(data.user_id);
            $('#' + showName).val(data.nickname);
        }
    })});
}
/* 选择任务老师 待完成 */
function selectTeacher(teacherNameId, teacherCodeId) {
    layui.use(['tableSelect'], function () {
    var tableSelect = layui.tableSelect;
    tableSelect.render({
        elem: '#' + (teacherNameId || '_teacherName') + ',#' + (teacherCodeId || "_teacherCode"),	//定义输入框input对象 必填
        checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'nickName',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '姓名',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url: context + '/api/sysuser/list',
            request: {
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            cols: [[
                // { type: 'checkbox' },//多选
                {type: 'radio'},//单选
                {field: 'user_id', title: '代码', width: 120},
                {field: 'nickName', title: '名称', width: 260},
                {field: 'schoolCode', title: '学校代码', width: 100}
            ]]
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            console.log(data);
            data = data.data[0];
            $('#' + (teacherNameId || '_teacherName')).val(data.schoolCode);
            $('#' + (teacherCodeId || '_teacherCode')).val(data.schoolName);
        }
    })});
}

//职责下拉动态填充
function iniDuty(v, formId) {
    $.ajax({
        type: 'get',
        url: context + "/api/sysDuty/list",
        success: function (response) {
            var data = response.data;
            if (typeof data == 'object') {
                var select = $('#' + (formId || 'dutySelect'));
                select.empty();
                var t = '<option value="0">全部</option>';
                for (var i = 0, n = data.length; i < n; i++) {
                    t += '<option value="' + data[i].duty_id + '">' + data[i].dutyName + '</option>';
                }
                select.append(t);
                // select.val(JSON.parse($('<div>').html('<%=_eduSchoolUser||"{}"%>').text()).duty_id || 0);
                select.val(v || 0);
                layui.form.render();
                console.log('duty finish');
            }
        }
    });
};

//教学计划分类下拉动态填充
function intPlanClassify(v, formId) {
    $.ajax({
        type: 'get',
        url: context + '/api/eduCoursePlanClassify/list',
        success: function (response) {
            var data = response.data;
            if (typeof data == 'object') {
                var select = $('#' + (formId || 'plan_classify_id'));
                select.empty();
                var t = '<option value="0">全部分类</option>';
                for (var i = 0, n = data.length; i < n; i++) {
                    t += '<option value="' + data[i].plan_classify_id + '">' + data[i].title + '</option>';
                }
                select.append(t);
                // select.val(JSON.parse($('<div>').html('<%=_eduSchoolUser||"{}"%>').text()).duty_id || 0);
                select.val(v || 0);
                layui.form.render();
            }
        }
    });
};

//学校专业选择
function selectSpe(param) {
    var p = param || {};
    p.codeId = p.idName || "_speCode";
    p.titleId = p.codeName || '_speName';
    p.schoolCode = p.schoolCode || '';
    var tableSelect = layui.tableSelect;
    tableSelect.render({
        elem: '#' + (p.codeId) + ',#' + (p.titleId),	//定义输入框input对象 必填
        checkedKey: 'speciality_id', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'title',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '专业名称',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url: (context + '/api/eduSchoolSpeciality/list'),
            request: {limitName: 'pageSize'},
            where: {schoolCode: p.schoolCode},
            cols: [[
                // { type: 'checkbox' },//多选
                {type: 'radio'},//单选
                {field: 'speciality_id', title: '代码', width: 120},
                {field: 'title', title: '名称', width: 200},
                {field: 'schoolCode', title: '学校', width: 200}
            ]]
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            data = data.data[0];
            $('#' + p.codeId).val(data.speciality_id);
            $('#' + p.titleId).val(data.title);
        }
    })
}
